package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modle.Event;

/**
 * Data access object (DAO) for domain model class User.
 * 
 * @see hibernateDao.User
 * @author MyEclipse Persistence Tools
 */

public class EventDao extends BaseDao {
	// property constants
	public static final String EVENT_NAME = "eventName";
	public static final String EVENT_TIME = "eventTime";
	public static final String EVENT_RECYCLE = "eventRecycle";
	public static final String EVENT_INTRODUCTION = "eventIntroduction";
	public static final String USER_ID = "userId";
	public static final String IS_SOUND_ALERT = "isSoundAlert";
	public static final String IS_WINDOW_ALERT = "isWindowAlert";
	public static final String SOUND_FILE_PATH = "soundFilePath";

	public int delete(Event persistentInstance) {
		try {
			String sql = "delete from event where eventId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, persistentInstance.getEventId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	public List<Event> findAll() {
		try {
			String sql = "select * from event";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Event> findByEventIntroduction(Object eventIntroduction) {
		return findByProperty(EVENT_INTRODUCTION, eventIntroduction);
	}

	public List<Event> findByEventName(Object eventName) {
		return findByProperty(EVENT_NAME, eventName);
	}

	public List<Event> findByEventRecycle(Object eventRecycle) {
		return findByProperty(EVENT_RECYCLE, eventRecycle);
	}

	public List<Event> findByEventTime(Object eventTime) {
		return findByProperty(EVENT_TIME, eventTime);
	}

	public Event findById(java.lang.Integer id) {
		try {
			String sql = "select * from event where eventId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, id);
			List<Event> instanceList = this.getListByResultSet(ps
					.executeQuery());
			if (instanceList == null) {
				return null;
			}
			Event instance = instanceList.get(0);
			return instance;
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Event> findByISSOUNDALERT(Object isSoundAlert) {
		return findByProperty(IS_SOUND_ALERT, isSoundAlert);
	}

	public List<Event> findByIsWindowAlert(Object isWindowAlert) {
		return findByProperty(IS_WINDOW_ALERT, isWindowAlert);
	}

	public List<Event> findByProperty(String propertyName, Object value) {
		try {
			String sql = "select * from event where " + propertyName + "='"
					+ value + "'";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Event> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	@Override
	protected List<Event> getListByResultSet(ResultSet rs) {
		List<Event> list = new ArrayList<Event>();
		try {
			if (!rs.next()) {
				return null;
			}
			do {
				Event event = new Event();
				event.setEventId(rs.getInt("eventId"));
				event.setEventName(rs.getString("eventName"));
				event.setEventTime(rs.getTime("eventTime"));
				event.setEventRecycle(rs.getInt("eventRecycle"));
				event.setEventIntroduction(rs.getString("eventIntroduction"));
				event.setIsSoundAlert(rs.getByte("isSoundAlert"));
				event.setIsWindowAlert(rs.getByte("isWindowAlert"));
				event.setSoundFilePath(rs.getString("soundFilePath"));
				list.add(event);
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public int save(Event transientInstance) {
		try {
			String sql = "insert into event(eventName, eventTime, eventRecycle, eventIntroduction, userId, isSoundAlert, isWindowAlert,soundFilePath) values(?, ?, ?, ?, ?, ?,?,?)";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, transientInstance.getEventName());
			ps.setTime(2, transientInstance.getEventTime());
			ps.setInt(3, transientInstance.getEventRecycle());
			ps.setString(4, transientInstance.getEventIntroduction());
			ps.setInt(5, transientInstance.getUser().getUserId());
			ps.setByte(6, transientInstance.getIsSoundAlert());
			ps.setByte(7, transientInstance.getIsWindowAlert());
			ps.setString(8, transientInstance.getSoundFilePath());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	@Override
	protected void setSavePreparedStatement(PreparedStatement ps, Object obj)
			throws SQLException {

	}

	public int update(Event instance) {
		try {
			String sql = "update event set eventName=?, eventTime=?, eventRecycle=?, eventIntroduction=?, isSoundAlert=?, isWindowAlert=? ,soundFilePath=? where eventId=?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, instance.getEventName());
			ps.setTime(2, instance.getEventTime());
			ps.setInt(3, instance.getEventRecycle());
			ps.setString(4, instance.getEventIntroduction());
			ps.setByte(5, instance.getIsSoundAlert());
			ps.setByte(6, instance.getIsWindowAlert());
			ps.setString(7, instance.getSoundFilePath());
			ps.setInt(8, instance.getEventId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}
}